<template>
    <UnitBaseInfo :opType="opType" :opUnitInfo="opUnitInfo" v-if="(opType==1||opType==3)&&opUnitInfo.id"/>

    <el-card class="w-full mt-[10px]">
        <CardHead> 规章制度 </CardHead>

        <el-table v-loading="loading" :data="regulationList">
            <el-table-column label="制度名称" align="center" prop="name" />
            <el-table-column label="制度附件" align="center" prop="fileIds">
                <template #default="scope">
                    <div v-if="scope.row.fileIds" >
                      <div v-for="(item, index) in scope.row.fileIds.split(',')" :key = index>
                        <el-link type="primary" @click.prevent="download.resource(item)">{{item.split(/[\/\\]+/).pop()}}</el-link>
                      </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="上传时间" align="center" prop="uploadDate" width="180">
                <template #default="scope">
                <span>{{ parseTime(scope.row.uploadDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="文件说明" align="center" prop="note" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="opType==1&&opUnitInfo.id">
                <template #default="scope">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['research:regulation:edit']">编辑</el-button>
                <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['research:regulation:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
            
        <pagination
            v-show="total>0&&false"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />
    </el-card>

    <el-form ref="regulationRef" :model="form" :rules="rules" label-width="96px" v-if="opType==1&&opUnitInfo.id">
        <el-card class="w-full mt-[10px]">
            <CardHead> {{title}} </CardHead>

            <el-row :gutter="20">
                <el-col :span="1"></el-col>
                <el-col :span="16">
                  <el-form-item label="制度名称" prop="name">
                      <el-input v-model="form.name" placeholder="请输入制度名称" />
                  </el-form-item>
                </el-col>
                <el-col :span="7"></el-col>
            </el-row>

            <el-row :gutter="20">
                <el-col :span="1"></el-col>
                <el-col :span="16">
                  <el-form-item label="上传时间" prop="uploadDate">
                      <el-date-picker clearable
                          v-model="form.uploadDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择上传时间">
                      </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="7"></el-col>
            </el-row>

            <el-row :gutter="20">
                <el-col :span="1"></el-col>
                <el-col :span="16">
                  <el-form-item label="制度附件" prop="fileIds">
                      <file-upload v-model="form.fileIds" :limit=1 :fileSize=20 :fileType="['doc','docx','xls','xlsx','pdf']" :isShowTip="true"/>
                  </el-form-item>
                </el-col>
                <el-col :span="7"></el-col>
            </el-row>

            <el-row :gutter="20">
                <el-col :span="1"></el-col>
                <el-col :span="16">
                  <el-form-item label="文件说明" prop="note">
                      <el-input v-model="form.note" type="textarea" placeholder="请输入内容" />
                  </el-form-item>
                </el-col>
                <el-col :span="7"></el-col>
            </el-row>
        </el-card>
    </el-form>
</template>

<script setup name="Regulation">
import cloneDeep from 'lodash/cloneDeep';  
import useUserStore from '@/store/modules/user';
import CardHead from "./CardHead.vue";
import UnitBaseInfo from "./UnitBaseInfo.vue";
import { listUnit, getUnit, delUnit, addUnit, updateUnit, getCategoryData } from "@/api/research/unit";
import { listRegulation, getRegulation, delRegulation, addRegulation, updateRegulation } from "@/api/research/regulation";
import download from '@/plugins/download'

const { proxy } = getCurrentInstance();

const emit = defineEmits(['refreshUnitList']);  
const props = defineProps({
  opType: {  
    type: Number,
    required: true,
    default: 0      // 默认值  0:add 1:edit 2:check 3:view
  },
  opUnitInfo: {  
    type: Object,
    required: false,
  }
}) 

const regulationList = ref([]);
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 1000,
    researchUnitId: props.opUnitInfo.id,
  },
  rules: {
    name: [
      { required: true, message: "制度名称不能为空", trigger: "blur" }
    ],
    fileIds: [
      { required: true, message: "制度附件不能为空", trigger: "blur" }
    ],
    uploadDate: [
      { required: true, message: "上传时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

onMounted(() => {
  handleAdd();
})

/** 查询研究机构-规章制度列表 */
function getList() {
  loading.value = true;
  listRegulation(queryParams.value).then(response => {
    regulationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 表单重置
function reset() {
  form.value = {
    ID: null,
    researchUnitId: props.opUnitInfo.id,    //researchUnitId: null,
    name: null,
    fileIds: null,
    uploadDate: null,
    note: null,
    CREATEUSERID: null,
    LASTEDITUSERID: null,
    CREATEDATE: null,
    LASTEDITDATE: null,
    CREATEUSERNAME: null,
    LASTEDITUSERNAME: null
  };
  proxy.resetForm("regulationRef");
}

function handleAdd() {
  reset();
  title.value = "规章制度新增";
}

function handleUpdate(row) {
  reset();
  const _ID = row.id || ids.value
  getRegulation(_ID).then(response => {
    form.value = response.data;
    title.value = "规章制度编辑";
  });
}

function handleDelete(row) {
  const _IDs = row.id || ids.value;
  proxy.$modal.confirm('您确定要删除吗?').then(function() {
    return delRegulation(_IDs);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
    handleAdd();
  }).catch(() => {});
}

function save() {
  proxy.$refs["regulationRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRegulation(form.value).then(response => {
            proxy.$modal.msgSuccess("编辑成功");
            getList();
            handleAdd();
            // proxy.$modal.confirm('是否继续操作？').then(function() {
            //     handleAdd();
            // }).catch(() => {
            //     cancel();
            //     emit('refreshUnitList');
            // });
        });
      } else {
        addRegulation(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          getList();
          handleAdd();
          // proxy.$modal.confirm('是否继续操作？').then(function() {
          //     handleAdd();
          // }).catch(() => {
          //     cancel();
          //     emit('refreshUnitList');
          // });
        });
      }
    }
  });
}

function cancel() {
    reset();
}

defineExpose({save,cancel});
getList();
</script>

<style scoped lang="scss">
@import url("../index.scss");
</style>